官方解释
Positions in RecyclerView:
RecyclerView introduces an additional level of abstraction between the RecyclerView.Adapter and RecyclerView.LayoutManager to be able to detect data set changes in batches during a layout calculation(计算)
. This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.
For this reason, there are two types of position related methods in RecyclerView:
- layout position: Position of an item in the latest layout calculation. This is the position from the LayoutManager’s perspective.
- adapter position: Position of an item in the adapter. This is the position from the Adapter’s perspective.
These two positions are the same except the time between dispatching adapter.notify* events and calculating the updated layout.
Methods that return or receive LayoutPosition use position as of the latest layout calculation (e.g. getLayoutPosition(), findViewHolderForLayoutPosition(int)). These positions include all changes until the last layout calculation. You can rely on these positions to be consistent with what user is currently seeing on the screen. For example, if you have a list of items on the screen and user asks for the 5th element, you should use these methods as they’ll match what user is seeing.
The other set of position related methods are in the form of AdapterPosition. (e.g. getAdapterPosition(), findViewHolderForAdapterPosition(int)) You should use these methods when you need to work with up-to-date adapter positions even if they may not have been reflected to layout yet. For example, if you want to access the item in the adapter on a ViewHolder click, you should use getAdapterPosition(). Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged() has been called and new layout has not yet been calculated. For this reasons, you should carefully handle NO_POSITION or null results from these methods.
When writing a RecyclerView.LayoutManager you almost always want to use layout positions whereas when writing an RecyclerView.Adapter, you probably want to use adapter positions
问题一: lint-error-do-not-treat-position-as-fixed-only-use-immediately
1 |
|
lint-error-do-not-treat-position-as-fixed-only-use-immediately
recyclerview-adapter-lint-error-do-not-treat-position-as-fixed1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
EffectData data = dataSets.get(position);
if (data.getCoverPic() == null) {
holder.idIndexItemImage.setImageResource(data.getImageID());
} else {
Glide.with(context)
.load(data.getCoverPic())
.placeholder(R.drawable.ic_filter1)
.into(holder.idIndexItemImage);
}
holder.idIndexItemText.setText(data.getText());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getAdapterPosition();
mOnItemClickListener.onItemClick(holder, pos);
select(pos);
}
});
if (data.isSelected()) {
holder.itemView.setSelected(true);
} else {
holder.itemView.setSelected(false);
}
}